using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace TidhiGanitam.DataModel
{
    public class VarjyamuRoutineItem
    {
        private int m_Year;
        DateTime m_Date;
        private int m_SNo;
        NakshatramuRoutineItem m_NakshatramuDetails;
        DinamuRoutineItem m_DinamuRoutines;
        ArrayList m_VarjyamuDetails;

        public int Bhagam
        {
            get { return m_SNo; }
        }

        public DateTime Date
        {
            get { return m_Date; }
        }

        public ArrayList VarjyamuDetails
        {
            get
            { return m_VarjyamuDetails; }
        }

        public VarjyamuRoutineItem(int SNo, int Year, DateTime Date, NakshatramuRoutineItem NakshatramuDetails,
            DinamuRoutineItem CurrentDinamuRoutine)
        {
            m_SNo = SNo;
            m_Year = Year;
            m_Date = Date;
            m_NakshatramuDetails = NakshatramuDetails;
            m_DinamuRoutines = CurrentDinamuRoutine;
            m_VarjyamuDetails = new ArrayList();
        }
        public void Calculate(DinamuRoutineItem PreviousDinamuRoutine, NakshatramuRoutineItem PreviousNakshatramuRoutine,
            VarjyamuRoutineItem PreviousVarjyamuRoutine)
        {
            //Add previous day's Next day varjyamu to current day varjyamu
            //foreach (VarjyamuDetails PrNxVarjyamu in PreviousVarjyamuRoutine.NextDayVarjyamuDetails)
            //{
            //    m_VarjyamuDetails.Add(PrNxVarjyamu);
            // }
             int NakshatramuIndex = -1;
            foreach (NakshatramuDetails Nakshatramu in m_NakshatramuDetails.NakshatramuDetails)
            {
                if (Nakshatramu.NakshatramuState == Enums.TidhiState.Purti)
                {
                    continue;
                }
                double[] dMultipliers = (double[])Constants.Varjyamu[Convert.ToInt32(Nakshatramu.NakshatramuIndex)];
                foreach (double dMultiplier in dMultipliers)
                {
                    Rasi VarjyamValue = new Rasi(Nakshatramu.TotalSpan * dMultiplier);
                    Rasi TyajyamuTime = new Rasi(Nakshatramu.TotalSpan / 15);
                    /* Changes start */
                    //We need to find out Nakshatramu time in previous day.
                    //This can be obtained by subtracting Current nakshatramu today span from Total Span
                    //We need to find out previous Nakshatramu time.
                    Rasi NakshatramPreviousValue = new Rasi();
                    if (NakshatramuIndex > -1)
                    {
                        NakshatramPreviousValue = new Rasi(((NakshatramuDetails)m_NakshatramuDetails.NakshatramuDetails[NakshatramuIndex]).EndTime);
                    }
                    else
                    {
                        foreach (NakshatramuDetails nak in PreviousNakshatramuRoutine.NakshatramuDetails)
                        {
                            NakshatramPreviousValue += nak.EndTime;
                        }
                    }
                    if (PreviousNakshatramuRoutine.PreviousNakshatramuState == Enums.TidhiState.Purti)
                    {
                        Rasi TwoDaysBackValue = Nakshatramu.TotalSpan - Nakshatramu.EndTime - PreviousNakshatramuRoutine.NakshatramuTime;
                        VarjyamValue = VarjyamValue - TwoDaysBackValue;
                        NakshatramPreviousValue = new Rasi();
                    }
                    //Find Total Varjyamu
                    Rasi TotalVarjyamuTime = VarjyamValue + NakshatramPreviousValue;

                    if (Nakshatramu.NakshatramuState == Enums.TidhiState.Eshyam)
                    {
                        VarjyamuDetails NxtVarjyamuItem = new VarjyamuDetails();
                        NxtVarjyamuItem.NakshatramuIndex = Nakshatramu.NakshatramuIndex;
                        NxtVarjyamuItem.VarjyamuTime = new Rasi(TotalVarjyamuTime);
                        NxtVarjyamuItem.VarjyamuEndTime = new Rasi(TotalVarjyamuTime + TyajyamuTime);
                        NxtVarjyamuItem.TyajyamuTime = new Rasi(TyajyamuTime);
                        NxtVarjyamuItem.VarjyamuSamayamu = GetVarjyamuSamayamu(TotalVarjyamuTime, PreviousDinamuRoutine);
                        m_VarjyamuDetails.Add(NxtVarjyamuItem);
                    }
                    else if (TotalVarjyamuTime > PreviousDinamuRoutine.RojuPramanam)
                    {
                        //Scenario 1
                        //Subtract Previous Dina Pramanamu from this
                        Rasi VarjyamuTime = TotalVarjyamuTime - PreviousDinamuRoutine.RojuPramanam;
                        VarjyamuDetails VarjyamuItem = new VarjyamuDetails();
                        VarjyamuItem.NakshatramuIndex = Nakshatramu.NakshatramuIndex;
                        VarjyamuItem.VarjyamuTime = new Rasi(VarjyamuTime);
                        VarjyamuItem.VarjyamuEndTime = new Rasi(VarjyamuTime + TyajyamuTime);
                        VarjyamuItem.TyajyamuTime = new Rasi(TyajyamuTime);
                        VarjyamuItem.VarjyamuSamayamu = GetVarjyamuSamayamu(VarjyamuTime, PreviousDinamuRoutine);
                        m_VarjyamuDetails.Add(VarjyamuItem);
                    }
                    else
                    {
                        //Scenario 2
                        //If Total AmrutaGhadiyalu time is less than previous roju pramanamu then
                        //AmrutaGhadiyalu goes to previous day
                        VarjyamuDetails PrVarjyamuItem = new VarjyamuDetails();
                        PrVarjyamuItem.NakshatramuIndex = Nakshatramu.NakshatramuIndex;
                        PrVarjyamuItem.VarjyamuTime = new Rasi(TotalVarjyamuTime);
                        PrVarjyamuItem.VarjyamuEndTime = new Rasi(TotalVarjyamuTime + TyajyamuTime);
                        PrVarjyamuItem.TyajyamuTime = new Rasi(TyajyamuTime);
                        PrVarjyamuItem.VarjyamuSamayamu = GetVarjyamuSamayamu(TotalVarjyamuTime, PreviousDinamuRoutine);
                        //Add this item to previous day's AmrutaGhadiyalu details
                        PreviousVarjyamuRoutine.VarjyamuDetails.Add(PrVarjyamuItem);
                    }

                    /* Changes End */
                }
                NakshatramuIndex++;
            }
        }

        private Enums.VarjyamuTime GetVarjyamuSamayamu(Rasi TotalVarjyamuTime, DinamuRoutineItem Dinamu)
        {
            Enums.VarjyamuTime VarjyamuTime = Enums.VarjyamuTime.Di_Vi;
            if (Dinamu.DinaPramanam < TotalVarjyamuTime)
            {
                VarjyamuTime = Enums.VarjyamuTime.Ra_Vi;
            }
            return VarjyamuTime;
        }
    }
}
